/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: DiscardShopStyle.java
 * @Package phoenics.apeak.simple.service
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:30
 * @version V1.0
 */
package phoenics.apeak.simple.service;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import phoenics.apeak.crawl.Discard;
import phoenics.apeak.crawl.Document;
import phoenics.apeak.simple.entity.UrlInfo;
import phoenics.apeak.simple.store.UrlInfoDao;
// TODO: Auto-generated Javadoc

/**
 * The Class DiscardShopStyle.
 *
 * @Description The Class DiscardShopStyle.
 */
@Service
public class DiscardShopStyle implements Discard{
	
	/** The url info dao. */
	@Autowired
	 UrlInfoDao urlInfoDao;
	
	/** The goods mapper. */
	ObjectMapper goodsMapper = new ObjectMapper();
	
	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Discard#colseDocument(phoenics.apeak.crawl.Document)
	 */
	@Override
	public void colseDocument(Document event) {
		String params=event.getParams();
		if (params == null) {
			return;
		}
		try {
			JsonNode rootNode = goodsMapper.readTree(params);
			ObjectNode metadata = (ObjectNode) rootNode;
			String category = metadata.get("category").asText();
			if(StringUtils.isNotEmpty(category)){
				updateUrlInfo4Error(category);
			}
		} catch (IOException e) {
		}
	}
	
	/**
 * Update url info4 error.
 *
 * @param category
 *            the category
 * @Description Update url info4 error.
 */
	private void updateUrlInfo4Error(String category) {
		int timeouts=timeouts(category);
		Query query = new Query();
		query.addCriteria(Criteria.where("id").is(category));
		Update update = new Update();
		update.set("over", 1);
		update.set("timeout", ++timeouts);
		urlInfoDao.updateFirst(query, update);
	}
	
	private int timeouts(String category){
		Query query = new Query();
		query.addCriteria(Criteria.where("id").is(category));
		UrlInfo urlInfo=urlInfoDao.findOne(query);
		return urlInfo.getTimeout();
	}
}
